<template>
    <div>
        <el-card style="margin-bottom: 20px">
        <el-breadcrumb  separator-class="el-icon-arrow-right" style="margin-bottom: 20px" >
            <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>留言板</el-breadcrumb-item>
            <el-breadcrumb-item>留言板管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入"  v-model="search"
        v-on:keyup.enter.native="text">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <el-table :data="tableData">
        <el-table-column label="创建时间" prop="createTime"> </el-table-column>
        <el-table-column label="留言人id" prop="user_id"> </el-table-column>
        <el-table-column label="用户名" prop="username" ></el-table-column>
        <el-table-column label="留言内容" prop="content"></el-table-column>
        <el-table-column label="留言图片" prop="picture">
          <template slot-scope="scope">
            <img width="100px" :src="scope.row.picture" alt="" />
          </template>
        </el-table-column>
         <el-table-column label="回复内容" prop="reply"></el-table-column>
        <el-table-column label="回复图片" prop="reply_picture"></el-table-column>
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button  type="primary"  size="mini"  @click="handleEdit(scope.$index, scope.row)" >编辑</el-button>
            <el-button type="danger"  size="mini" @click="handleDel(scope.$index, scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
         <el-pagination
          @current-change="current"
          layout="prev, pager, next"
          :page-size="pageSize"
          background
          :total="total"
        >
        </el-pagination>
    </div>
    </el-card>

    <!-- 添加留言板 -->
    <el-dialog title="添加留言板" :visible.sync="addDialogVisible" width="50%">
      <el-form :model="addForm" >
        <el-form-item label="创建时间">
          <el-input v-model="addForm.createTime"></el-input>
        </el-form-item>
        <el-form-item label="留言人id">
          <el-input v-model="addForm.user_id"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="留言内容">
          <el-input v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="留言图片">
          <el-upload
            class="upload-demo"
            action="action"
            :auto-upload="true"
            :limit="1"
            ref="upload"
            accept="image/jpeg,image/png,image/jpg"
            :http-request="uploadFile"
            multiple>
            <el-button type="primary" size="small">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传图片</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input v-model="addForm.reply"></el-input>
        </el-form-item>
        <el-form-item label="回复图片">
          <el-input v-model="addForm.reply_picture"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">提交</el-button>
      </span>
    </el-dialog>

    <!-- 编辑留言板 -->
    <el-dialog title="编辑留言板" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm" >
        <el-form-item label="创建时间">
          <el-input v-model="editForm.createTime"></el-input>
        </el-form-item>
        <el-form-item label="留言人id">
          <el-input v-model="editForm.user_id"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="留言内容">
          <el-input v-model="editForm.content"></el-input>
        </el-form-item>
        <el-form-item label="留言图片:">
          <el-input  v-model="editForm.picture"></el-input>
        </el-form-item>
        <el-input type="hidden" v-model="editForm.picture"></el-input>
        <el-form-item label="回复内容">
          <el-input v-model="editForm.reply"></el-input>
        </el-form-item>
        <el-form-item label="回复图片">
          <el-input  v-model="editForm.reply_picture"></el-input>
        </el-form-item>
        <!-- <el-input type="hidden" v-model="editForm.replyPicture"></el-input> -->
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editUser">提交</el-button>
      </span>
    </el-dialog>

    </div>
</template>

<script>
export default {
  data() {
    return {
     total: 3,
     search:'',
      editDialogVisible: false,
      addDialogVisible: false,
      addForm: {},
      editForm: {},
      tableData: [],
      pageNum: 1,
      pageSize: 3,
      nowImg: "",
    }
  },
  methods: {
      async getList(pageNum, pageSize) {
      //  let { data: res } = await this.$axios.get('data/fupin.json')
      //     window.console.log(res)
      //     this.tableData = res.data.list;
      let { data: res } = await this.$axios.get("/msgboard/getlist", {
        params: {
          pageSize,
          pageNum,
        },
      })
      window.console.log(res);
      this.total = res.data.total
      this.tableData = res.data.list;
    },
     async addUser() {
      this.addForm.picture = this.nowImg
      let { data: res } = await this.$axios.post("/msgboard/add",this.addForm);
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.addDialogVisible = false;
    },
    async editUser() {
      let { data: res } = await this.$axios.post(
        "/msgboard/update",
        this.editForm
      );
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.editDialogVisible = false;
    },
    async handleEdit(index, row) {
      let { data: res } = await this.$axios.get("/msgboard/getlist", {
        params: {
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          id:row.id
        },
      });
      this.editForm = res.data.list[0];
      this.editDialogVisible = true;
    },
    async uploadFile() {
      const formData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = { headers: { "Content-Type": "multipart/form-data" } };
      if (!file) {
        this.$message.error("请选择文件");
        return;
      }
      window.console.log('图片解析为：'+file.raw)
      formData.append("file", file.raw);
      formData.append("type", 'YONGHU');
      let { data: res } = await this.$axios.post("/uploadImg",formData, headerConfig );
      window.console.log(res)
      this.nowImg = res.data.imgUrl
    },
    async handleDel(index, id) {
      //window.console.log(index+"  "+id)
      let { data: res } = await this.$axios.get("/msgboard/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.message);
      this.getList(this.pageNum, this.pageSize);
    },
    current(e) {
      this.pageNum = e;
      this.getList(e, this.pageSize);
    },
  },
  created() {
    this.getList(this.pageNum,this.pageSize);
  },
};
</script>
